// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.zzx.settings;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * Provides controller functionality for application settings.
 */
public class AppSettingsConfigurable implements Configurable {

  private AppSettingsComponent mySettingsComponent;

  // A default constructor with no arguments is required because this implementation
  // is registered as an applicationConfigurable EP

  @Nls(capitalization = Nls.Capitalization.Title)
  @Override
  public String getDisplayName() {
    return "Maven私服上传";
  }

  @Nullable
  @Override
  public JComponent createComponent() {
    mySettingsComponent = new AppSettingsComponent();
    return mySettingsComponent.getPanel();
  }

  @Override
  public boolean isModified() {
    AppSettingsState settings = AppSettingsState.getInstance();
    if(StringUtil.isEmpty(mySettingsComponent.getRepositoryText())){
      mySettingsComponent.setMessageText("本地Maven仓库地址不能为空!");
      return false;
    }
    if(StringUtil.isEmpty(mySettingsComponent.getUsernameText())){
      mySettingsComponent.setMessageText("Maven私服用户名不能为空!");
      return false;
    }
    if(StringUtil.isEmpty(mySettingsComponent.getNexusText())){
      mySettingsComponent.setMessageText("Maven私服地址不能为空!");
      return false;
    }
    if(StringUtil.isEmpty(mySettingsComponent.getPasswordText())){
      mySettingsComponent.setMessageText("Maven私服密码不能为空!");
      return false;
    }
    mySettingsComponent.setMessageText("");
    boolean modified = !mySettingsComponent.getNexusText().equals(settings.nexus);
    modified |= !mySettingsComponent.getRepositoryText().equals(settings.repository);
    modified |= !mySettingsComponent.getUsernameText().equals(settings.username);
    modified |= !mySettingsComponent.getPasswordText().equals(settings.password);
    return modified;

  }

  @Override
  public void apply() {
    AppSettingsState settings = AppSettingsState.getInstance();
    if(StringUtil.isEmpty(mySettingsComponent.getRepositoryText())){
      Messages.showMessageDialog("本地Maven仓库地址不能为空","错误信息",Messages.getErrorIcon());
    }
    if(StringUtil.isEmpty(mySettingsComponent.getUsernameText())){
      Messages.showMessageDialog("Maven私服用户名不能为空","错误信息",Messages.getErrorIcon());
    }
    if(StringUtil.isEmpty(mySettingsComponent.getNexusText())){
      Messages.showMessageDialog("Maven私服地址不能为空","错误信息",Messages.getErrorIcon());
    }
    if(StringUtil.isEmpty(mySettingsComponent.getPasswordText())){
      Messages.showMessageDialog("Maven私服密码不能为空","错误信息",Messages.getErrorIcon());
    }
    settings.repository = mySettingsComponent.getRepositoryText();
    settings.username = mySettingsComponent.getUsernameText();
    settings.nexus = mySettingsComponent.getNexusText();
    settings.password = mySettingsComponent.getPasswordText();
  }

  @Override
  public void reset() {
    AppSettingsState settings = AppSettingsState.getInstance();
    mySettingsComponent.setRepositoryText(settings.repository);
    mySettingsComponent.setNexusText(settings.nexus);
    mySettingsComponent.setUsernameText(settings.username);
    mySettingsComponent.setPasswordText(settings.password);
  }

  @Override
  public void disposeUIResources() {
    mySettingsComponent = null;
  }

}
